#include <math.h>
#include <stdio.h>

struct complex_struct {
	double x, y;
};

double real_part(struct complex_struct z)
{
	return z.x;
}

double img_part(struct complex_struct z)
{
	return z.y;
}

double magnitude(struct complex_struct z)
{
	return sqrt(z.x * z.x + z.y * z.y);
}

double angle(struct complex_struct z)
{
	return atan2(z.y, z.x);
}

struct complex_struct make_from_real_img(double x, double y)
{
	struct complex_struct z;
	z.x = x;
	z.y = y;
	return z;
}

struct complex_struct make_from_mag_ang(double r, double A)
{
	struct complex_struct z;
	z.x = r * cos(A);
	z.y = r * sin(A);
	return z;
}
struct complex_struct add_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_real_img(real_part(z1) + real_part(z2),
				  img_part(z1) + img_part(z2));
}

struct complex_struct sub_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_real_img(real_part(z1) - real_part(z2),
				  img_part(z1) - img_part(z2));
}

struct complex_struct mul_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_mag_ang(magnitude(z1) * magnitude(z2),
				 angle(z1) + angle(z2));
}

struct complex_struct div_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_mag_ang(magnitude(z1) / magnitude(z2),
				 angle(z1) - angle(z2));
}
// 打印复数的函数
void print_complex(struct complex_struct z) {
    double real = real_part(z);
    double imag = img_part(z);

    if (real == 0 && imag == 0) {
        printf("0.0");
        return;
    }

    if (real != 0) {
        printf("%.1f", real);
    }

    if (imag != 0) {
        if (imag > 0 && real != 0) {
            printf("+");
        }
        printf("%.1fi", imag);
    }

    printf("\n"); // 换行
}

int main(void){
    
    struct complex_struct z1 = make_from_real_img(3.0, 2.0); 
    struct complex_struct z2 = make_from_real_img(1.5, -4.0);

    struct complex_struct sum = add_complex(z1, z2);

    print_complex(sum);

    return 0;


}
